<?php
function PdfGeneratorQueryUrl($id) {
    $sql = sprintf( "SELECT * FROM `SimpleEvents` WHERE id='%d' ", $id);
    $res = mysql_query($sql) or die(mysql_error());
    $row =  mysql_fetch_assoc($res) or die(mysql_error());


    if (isset($_SESSION['pinfo'])) $pinfo = $_SESSION['pinfo']; 
    else {
        $pinfo =  $_SESSION['Global']['People'][$row['person']];
    }

    //print_r($row);

    $od = date("j. n.",strtotime($row['DateBegin']));
    $do = date("j. n.",strtotime($row['DateEnd']));
    $odY = date("j. n. Y",strtotime($row['DateBegin']));
    $doY = date("j. n. Y",strtotime($row['DateEnd']));
    $dne = date("j. n. Y",strtotime($row['DateCreated']));
    $dnu = $row['WorkDays'];
    
    if ($dnu<=0 and $row['type']==1) {
        echo "Schází délka dovolené!";
        $dnu='';
    }    
    
    $gs = SQLGrantyEvent($id);
    $granty = "";
    foreach ($gs as $g) { //print_r($row);
        $granty = $granty .  $g['Name'] . " ";
    }

    $data = array('jmeno'=>PersonName($pinfo), 
                    'id'=>$id,
                    'rok'=>date("y", strtotime($row['DateBegin'])),
                    'od'=>$od,
                    'do'=>$do,
                    'odY'=>$odY,
                    'doY'=>$doY,
                    'dnu'=>$dnu,
                    'dne'=>$dne,
                    'adresa' => $pinfo['Adress'],
                    'granty' => $granty,
                    'sql'=>$row);
    
    if ($row['type']==1) { // dovolenka musi byt tisknuta pro kazdy mesic zvlast
        $m1 = date("n",strtotime($row['DateBegin']));
        $m2 = date("n",strtotime($row['DateEnd']));
        $y1 = date("y",strtotime($row['DateBegin']));
        $y2=date("y",strtotime($row['DateEnd']));
        $pocet_mesicu = $m2 - $m1 + 12* ( $y2 - $y1);
        $y_i = $y1;
        $dnu_celkem = 0;
        for ($i=0; $i<=$pocet_mesicu; $i++){
            $m_i = ((($m1+$i) - 1) % 12) + 1;
            if (($m1+$i) == 13) $y_i++ ; 

            if ($i==0) 
                $den_od_i = date("j", strtotime($row['DateBegin'])); 
            else 
                $den_od_i = 1 ;  
            
            if ($i<$pocet_mesicu) 
                $den_do_i = cal_days_in_month(CAL_GREGORIAN, $m_i, 2000+$y_i); 
            else 
                $den_do_i = date("j", strtotime($row['DateEnd']));  
            
            $dnu_i = networkdays( mktime(0,0,0,   $m_i, $den_od_i, 2000+$y_i), 
                                  mktime(23,59,59,$m_i, $den_do_i, 2000+$y_i ));
            $dnu_celkem += $dnu_i;
            
            $data_mesicni[$i] = array(
                'od'  => sprintf("%s. %s.", $den_od_i, $m_i),
                'do'  => sprintf("%s. %s.", $den_do_i, $m_i),
                'dnu' => $dnu_i,
                'rok' => $y_i
               );
            
        }
        if ($dnu != $dnu_celkem) printf("Chyba, pracovni dny nesedi, %s <> %s !   ", $dnu, $dnu_celkem);
        $data['DovolenaMesicne'] = $data_mesicni;
    }
    
    
    mysql_free_result($res);
    
    $query = "http://barma.math.slu.cz/Archiv/Tisk.php?".http_build_query($data);
    return $query;
}

function GetPdfFileName($id) {
    return sprintf("%s.pdf", $id); 
}

function GetPdfUrl($id) {
    //return "http://barma.math.slu.cz/Archiv/Dokumenty/".GetPdfFileName($id);
   return "archiv/".GetPdfFileName($id);
}

function GeneratePdf($id) {
    $query = PdfGeneratorQueryUrl($id);

    $ch = curl_init($query);
    $fname=GetPdfFileName($id);
    $furl = sprintf("archiv/%s", $fname);
    $fp = fopen($furl, "w");

    curl_setopt($ch, CURLOPT_FILE, $fp);
    curl_setopt($ch, CURLOPT_HEADER, 0);

    $res = curl_exec($ch);

    //print_r($res);
    curl_close($ch);
    fclose($fp);

    return GetPdfUrl($id);
    
}


?>
